package com.qfmy.mystation.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.qfmy.mystation.R;
import com.qfmy.mystation.bean.WallpaperBean;
import com.qfmy.mystation.dbHelper.WallpaperDBHelper;
import com.qfmy.mystation.utils.SaveBitmapToDIMIUtil;
import com.qfmy.mystation.utils.SetWallpaperUtil;
import com.qfmy.mystation.utils.ToastUtils;

import java.util.List;

public class HorizontalVpAdapter extends RecyclerView.Adapter<HorizontalVpAdapter.HorizontalVpViewHolder> {
    private final Context mContext;
    private List<WallpaperBean> wallpapers;

    public HorizontalVpAdapter(Context context, List<WallpaperBean> wallpapers) {
        mContext = context;
        this.wallpapers = wallpapers;
    }

    @NonNull
    @Override
    public HorizontalVpViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new HorizontalVpViewHolder(LayoutInflater.from(mContext).inflate((R.layout.item_picture_detail), parent, false));
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull HorizontalVpViewHolder holder, int position) {
        WallpaperBean bean = wallpapers.get(position);
        Glide.with(mContext)
                .load(bean.getUrl())
                .into(holder.img);  // 加载图片到ImageView
        holder.img.setOnClickListener(view -> {
            if (holder.SHOW) {
                holder.fl_footer.setVisibility(View.VISIBLE);
                holder.btn_download.setVisibility(View.VISIBLE);
                holder.btn_collection.setVisibility(View.VISIBLE);
                holder.SHOW = false;
            } else {
                holder.fl_footer.setVisibility(View.GONE);
                holder.btn_download.setVisibility(View.GONE);
                holder.btn_collection.setVisibility(View.GONE);
                holder.SHOW = true;
            }
        });
        holder.set_as_lock_paper.setOnClickListener(view -> {
            holder.img.buildDrawingCache();
            Bitmap bitmap = holder.img.getDrawingCache();
            SetWallpaperUtil.setWallpaper(mContext, bitmap, SetWallpaperUtil.FLAG_LOCK);
            ToastUtils.shortToast("锁屏壁纸设置成功");
        });
        holder.set_as_desktop_paper.setOnClickListener(view -> {
            holder.img.buildDrawingCache();
            Bitmap bitmap = holder.img.getDrawingCache();
            SetWallpaperUtil.setWallpaper(mContext, bitmap, SetWallpaperUtil.FLAG_SYSTEM);
            ToastUtils.shortToast("桌面壁纸设置成功");
        });
        holder.btn_download.setOnClickListener(view -> {
            holder.img.buildDrawingCache();
            Bitmap bitmap = holder.img.getDrawingCache();
            SaveBitmapToDIMIUtil.saveImage29(mContext, bitmap);
            Toast.makeText(mContext, "图片成功保存到相册", Toast.LENGTH_SHORT).show();
            WallpaperDBHelper.insert(mContext, bean.getTag(), bean.getUrl(), "", 0,
                    WallpaperDBHelper.TYPE_DOWNLOAD);
        });
        holder.btn_collection.setOnClickListener(view -> {
            Toast.makeText(mContext, "收藏成功", Toast.LENGTH_SHORT).show();
            WallpaperDBHelper.insert(mContext, bean.getTag(), bean.getUrl(), null, 0,
                    WallpaperDBHelper.TYPE_COLLECTION);
        });
        if (!TextUtils.isEmpty(bean.getTag())) {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            holder.rv_tag.setLayoutManager(linearLayoutManager);
            holder.rv_tag.setAdapter(new TagListAdapter(bean.getTag().split(","), R.layout.item_tag_detail));
        }
    }

    @Override
    public int getItemCount() {
        return wallpapers == null ? 0 : wallpapers.size();
    }

    class HorizontalVpViewHolder extends RecyclerView.ViewHolder {
        Boolean SHOW = true;
        ImageView img;
        Button set_as_lock_paper, set_as_desktop_paper;
        RecyclerView rv_tag;
        FrameLayout fl_footer;
        ImageButton btn_download;
        ImageButton btn_collection;

        HorizontalVpViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.image_detail);
            set_as_lock_paper = itemView.findViewById(R.id.set_as_lock_paper);
            set_as_desktop_paper = itemView.findViewById(R.id.set_as_desktop_paper);
            rv_tag = itemView.findViewById(R.id.rv_tag);
            fl_footer = itemView.findViewById(R.id.footer);
            btn_download = itemView.findViewById(R.id.btn_download);
            btn_collection = itemView.findViewById(R.id.btn_collection);
        }
    }
}

